package com.xingkeduo.business.operation.controller;

import com.alibaba.fastjson.JSON;
import com.xingkeduo.business.operation.domain.PostsPermsDomain;
import com.xingkeduo.pojo.json.PermsJson;
import com.xingkeduo.pojo.json.PostsPermsJson;
import com.xingkeduo.pojo.query.PostsPermsQuery;
import com.xingkeduo.pojo.table.PermsTable;
import com.xingkeduo.pojo.table.PostsPermsTable;
import com.xingkeduo.util.pojo.base.BaseController;
import com.xingkeduo.util.tool.ReturnUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.xingkeduo.tool.OperaConstants.CMS_URL_PREFIX;

/**
 * 职位-权限管理
 *
 * @author wangxilong <wangxilong@xingkeduo.com>
 * @version id:v0.1 ${date}${time}
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
@Slf4j
@RestController
@RequestMapping(CMS_URL_PREFIX +"/postsPerms")
public class PostsPermsController extends InitController {

    @Autowired
    private PostsPermsDomain postsPermsDomain;

    //查看
    @RequestMapping(value = "/{urlParam:.+}", method = RequestMethod.GET)
    public String doGet(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            PostsPermsQuery postsPermsQuery = PostsPermsJson.createQueryForPostsPerms(urlParam);
            long id = postsPermsQuery.getId();
            PostsPermsTable postsPermsTable = postsPermsDomain.getPostsPermsById(id);
            return returnUtil.getSuccess(PostsPermsJson.toJson(postsPermsTable));
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //添加
    @RequestMapping(value = "", method = RequestMethod.POST)
    public String doPost(@RequestBody @Valid PostsPermsTable postsPermsVo , ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
            //this.errMsg(bindingResult);
            postsPermsVo.setCreatedBy(this.getOperator(request));
            log.info("添加权限接口请求参数:{}", JSON.toJSONString(postsPermsVo));
            int postsPerms = postsPermsDomain.createPostsPerms(postsPermsVo);
            log.info("添加权限接口返回值:{}", JSON.toJSONString(postsPerms));
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //修改
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.PUT)
    public String doPut(@PathVariable String urlParam, @RequestBody @Valid PostsPermsTable postsPermsVo, ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
            PostsPermsQuery postsPermsQuery =  JSON.parseObject(urlParam,PostsPermsQuery.class);
            postsPermsVo.setUpdatedBy(this.getOperator(request));
            postsPermsVo.setPostsId(postsPermsQuery.getPostsId());
//            PostsPermsTable postsPermsTable = PostsPermsJson.createTableForModifyPostsPerms(urlParam,postsPermsVo);
            log.info("修改权限接口请求参数:{}", JSON.toJSONString(postsPermsVo));
            int postsPerms = postsPermsDomain.modifyPostsPerms(postsPermsVo);
            log.info("修改权限接口返回值:{}", JSON.toJSONString(postsPerms));
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //删除
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.DELETE)
    public String doDelete(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            PostsPermsQuery postsPermsQuery = PostsPermsJson.createQueryForPostsPerms(urlParam);
            long id = postsPermsQuery.getId();
            postsPermsDomain.delPostsPerms(id);
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //查看列表
    @RequestMapping(value = "/getAllPerms/{urlParam:.+}", method = RequestMethod.GET)
    public String doGetList(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
//            PostsPermsQuery postsPermsQuery = PostsPermsJson.createQueryForPostsPerms(urlParam);
            PostsPermsQuery postsPermsQuery =  JSON.parseObject(urlParam,PostsPermsQuery.class);

            List<PermsTable> permsTable = postsPermsDomain.getPostsPermsByPosts(postsPermsQuery);

            return returnUtil.getSuccess(PermsJson.listToJson(permsTable));
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

}
